﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using E4D.AppFabric.Entities.Engine;
using System.Management.Automation;

namespace UnitTests
{
    [TestClass]
    public class CreateSessionTest
    {
        private IList<Command> executedCommands = new List<Command>();

        ISessionFactory getMockSessionFactory()
        {
            return new Mocks.MockSessionFactory(executedCommands);
        }

        [TestCleanup()]
        public void Cleanup()
        {
            executedCommands.Clear();
        }

        [TestMethod]
        public void CreateSession()
        {
            //INIT
            SessionFactory.changeFactory(getMockSessionFactory());

            //ACT
            ISession localSession = SessionFactory.getLocalSession();
            ISession remoteSession = SessionFactory.getRemoteSesion("other1");

            //ASSERT
            Assert.IsNotNull(localSession);
            Assert.IsNotNull(remoteSession);
        }

        [TestMethod]
        public void TestInvoke()
        {
            //INIT
            SessionFactory.changeFactory(getMockSessionFactory());

            //ACT
            ISession localSession = SessionFactory.getLocalSession();
            localSession.Invoke("Use-CacheCluster");

            //ASSERT
            Assert.IsNotNull(localSession);
            Assert.IsTrue(executedCommands.Count == 1);
            Assert.IsTrue(executedCommands[0].Name == "Use-CacheCluster");
            Assert.IsNull(executedCommands[0].Parameters);
        }

        [TestMethod]
        public void TestInvokeWithParams()
        {
            //INIT
            SessionFactory.changeFactory(getMockSessionFactory());

            //ACT
            ISession localSession = SessionFactory.getLocalSession();
            localSession.Invoke("Use-CacheCluster", new Dictionary<String, object>() {{"Name", "Value"}});

            //ASSERT
            Assert.IsNotNull(localSession);
            Assert.IsTrue(executedCommands.Count == 1);
            Assert.IsTrue(executedCommands[0].Name == "Use-CacheCluster");
            Assert.IsNotNull(executedCommands[0].Parameters);
            Assert.IsTrue(executedCommands[0].Parameters.Count == 1);
            Assert.IsTrue(executedCommands[0].Parameters["Name"].Equals("Value"));
        }

    }
}
